package com.bj.crm.settings.web.interceptor;


import com.bj.crm.commons.constants.Constants;
import com.bj.crm.settings.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        //如果用户没有登录成功，则跳转到登录页面
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        if (user == null){
            response.sendRedirect(request.getContextPath());//重定向时，url必须加项目的名称
            return false;
        }
        return true;

//        //登录验证
//        //登录成功(session 中有 user) -》放行
//        if (request.getSession().getAttribute(Contants.SESSION_USER)!=null){
//            return true;
//        }
//        //登录失败-》跳转登录
//        response.sendRedirect(request.getContextPath()+"/");
//        return false;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
